home
diamond Go Premium
Data Engineering Path  ·  PySpark

DataFrame Join Operations

Joining multiple datasets in PySpark using standard SQL join types like inner, left, right, outer, semi, and anti joins.


What is the Join Operation?

The join() operation merges two DataFrames horizontally based on a matching key or expression, equivalent to relational database tables joins.

It is a critical transformation in relational modeling and distributed pipelines, where join types determine how unmatched records are preserved.


Syntax and Supported Join Types

# Standard Syntax
df_left.join(df_right, joinExpression, joinType)

Supported Join Types in PySpark:

  • "inner" (Default): Keeps only rows with keys matching in both sides.
  • "left" or "left_outer": Keeps all left rows, filling unmatched right side with nulls.
  • "right" or "right_outer": Keeps all right rows, filling unmatched left side with nulls.
  • "outer" or "full": Keeps all rows from both sides, aligning keys and using nulls for gaps.
  • "left_semi": Keeps left rows that have a match in the right side (keeps only left columns).
  • "left_anti": Keeps left rows that DO NOT have any match in the right side (ideal for finding missing/deleted keys).

Example Usage Pipeline

Below is a complete, copy-paste-ready PySpark script demonstrating joins:

from pyspark.sql import SparkSession
from pyspark.sql import functions as F

# 1. Setup local Spark session
spark = SparkSession.builder \
    .appName("DataFrame Joins Demo") \
    .master("local[*]") \
    .getOrCreate()

# 2. Dummy Left Dataset (Employees)
emp_data = [
    (1, "Alice", 101),
    (2, "Bob", 102),
    (3, "Charlie", 103),  # Dep ID 103 does not exist
    (4, "David", None),   # No department assigned
]
emp_columns = ["emp_id", "emp_name", "dept_id"]
emp_df = spark.createDataFrame(emp_data, emp_columns)

# 3. Dummy Right Dataset (Departments)
dept_data = [
    (101, "Engineering"),
    (102, "Marketing"),
    (104, "Finance"),      # No employees in Finance
]
dept_columns = ["dept_id", "dept_name"]
dept_df = spark.createDataFrame(dept_data, dept_columns)

# 4. Perform Inner Join (keeps only matching employees + depts)
inner_join = emp_df.join(dept_df, "dept_id", "inner")

# 5. Perform Left Join (keeps all employees, unmatched depts get Null)
left_join = emp_df.join(dept_df, "dept_id", "left")

# 6. Perform Left Anti Join (finds employees in departments that DO NOT exist or are null)
unmatched_employees = emp_df.join(dept_df, "dept_id", "left_anti")

# 7. Show results
print("=== Inner Join ===")
inner_join.show()

print("=== Left Outer Join ===")
left_join.show()

print("=== Left Anti Join (Orphaned Employees) ===")
unmatched_employees.show()

Rendered Output:

=== Inner Join ===
+-------+------+--------+-----------+
|dept_id|emp_id|emp_name|  dept_name|
+-------+------+--------+-----------+
|    101|     1|   Alice|Engineering|
|    102|     2|     Bob|  Marketing|
+-------+------+--------+-----------+

=== Left Outer Join ===
+-------+------+--------+-----------+
|dept_id|emp_id|emp_name|  dept_name|
+-------+------+--------+-----------+
|    101|     1|   Alice|Engineering|
|    102|     2|     Bob|  Marketing|
|    103|     3| Charlie|       null|
|   null|     4|   David|       null|
+-------+------+--------+-----------+

=== Left Anti Join (Orphaned Employees) ===
+------+--------+-------+
|emp_id|emp_name|dept_id|
+------+--------+-------+
|     3| Charlie|    103|
|     4|   David|   null|
+------+--------+-------+
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.